//
//  GameShowViewController.m
//  RecreationGame
//
//  Created by appying_wxj on 15/1/22.
//  Copyright (c) 2015年 appying_wxj. All rights reserved.
//

#import "GameShowViewController.h"
#import "GameDetailViewController.h"
@interface GameShowViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *_gameShowTableView;
    NSMutableArray *_fileArray;
}

@end

@implementation GameShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initGameShowUI];
    NSLog(@"-----------%@",self.objectIdStr);
    [self getGameShowData];
}

// 创建界面
-(void)initGameShowUI{
//    self.leftButton.backgroundColor = [UIColor colorWithRed:0.9 green:0.62 blue:0.67 alpha:1];
    [self.leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    _gameShowTableView = [[UITableView alloc] init];
    _gameShowTableView.frame = CGRectMake(0, 64, kwidth, kHeight-64);
    _gameShowTableView.delegate = self;
    _gameShowTableView.dataSource = self;
    [self.view addSubview:_gameShowTableView];
}
// 获取数据
-(void)getGameShowData{
    _titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    _fileArray = [[NSMutableArray alloc] initWithCapacity:0];
    AVQuery *titleQuery = [AVQuery queryWithClassName:@"classify"];
    AVObject *objectId = [titleQuery getObjectWithId:self.objectIdStr];
    AVRelation *contentsRelation = [objectId relationforKey:@"gameTitleRelation"];
    [[contentsRelation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (!error) {
//            NSLog(@"-------------------------%@",objects);
            
            for (int a = 0 ; a < objects.count; a++) {
                AVObject *allObject = [objects objectAtIndex:a];
                [_titleArray addObject:[allObject objectForKey:@"title"]];
                AVFile *file = [[objects objectAtIndex:a] objectForKey:@"htmlFile"];
                [_fileArray addObject:file.url];
            }
            NSLog(@"-------------获取到的文件是------------%@",_fileArray);
            [_gameShowTableView reloadData];
        }else{
            
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
    return cell;
}
// 点击单元格的时候的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GameDetailViewController *gameDetail = [[GameDetailViewController alloc] init];
    gameDetail.fileString = [_fileArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:gameDetail animated:YES];
}
-(void)leftButtonClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
